package com.yfeng.common.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

/**
 * 特殊字符过滤器
 * 跳转显示错误信息的页面可以统一
 *
 * @author rqn 
 */
public class SpecialCharacterFilter implements Filter {
    /**
     * 记录日志
     */
    private final static Log logger = LogFactory.getLog(SpecialCharacterFilter.class);

    public void destroy() {


    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {

        Enumeration enumration = request.getParameterNames();
        boolean haseSpecial = false;
        while (enumration.hasMoreElements()) {
            String pname = (String) enumration.nextElement();
            String inputString = request.getParameter(pname);
            if (Regular.haveSpecialCharacter(inputString)) {
                SpecialCharacterFilter.logger.info("----------------检查到输入参数" + pname + "的值包含特殊字符--------------------:" + inputString);
                haseSpecial = true;
                break;
            }
        }
        if (haseSpecial)//到统一的页面给用户提示信息
        {
            //((HttpServletResponse)response).sendRedirect("../sjyd/error.jsp");
            response.setContentType("text/javascript;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.write("<script>alert('输入内容中有非法字符单引号!!');</script>");
        } else
            filterChain.doFilter(request, response);
    }

    public void init(FilterConfig arg0) throws ServletException {

    }

}

class Regular {
    /**
     * 使用正则表达式检验输入参数的值是否有特殊字符
     *
     * @param inputString
     * @return
     */
	private static final Log log=LogFactory.getLog(Regular.class);
    public static boolean haveSpecialCharacter(String inputString) {
        PatternCompiler patternCompiler = new Perl5Compiler();
        PatternMatcher matcher = new Perl5Matcher();
        String regexpSpecial = "\'+";//校验规则,包含'
        Pattern pattern = null;
        try {
            pattern = patternCompiler.compile(regexpSpecial);
        } catch (MalformedPatternException e) {
            log.error(e.getMessage(),e);
        }
        if (matcher.contains(inputString, pattern)) {
            //MatchResult result=matcher.getMatch();
            return true;
        }
        return false;
	}
}